、using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;namespace FindWindow{ public partial class Form1 : Form { IntPtr hwnd = new IntPtr(0); public string czct = "开具增值税专用发票"; public string fpkjpp = "开具增值税普通发票"; // const int WM_COPYDATA = 0x004A;//必须是这个数值,不能更改 const int WM_COPYDATA = 0x13; const int WM_SETTEXT = 0x000C; List<string> list = new List<string>(); [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)] public static extern int SendMessage(IntPtr hwnd, uint wMsg, int wParam, string lParam); [DllImport("user32.dll")] public static extern int EnumChildWindows(IntPtr hWndParent, CallBack lpfn, int lParam); public delegate bool CallBack(IntPtr hwnd, int lParam); [DllImport("user32.dll")] private static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, int lParam); private delegate bool WNDENUMPROC(IntPtr hWnd, int lParam); [DllImport("user32.dll")] private static extern int GetWindowTextW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount); [DllImport("user32.dll")] public static extern int GetWindowText(int hWnd, IntPtr lpString, int nMaxCount); [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount); [DllImport("User32.dll", EntryPoint = "SendMessage")] private static extern int SendMessage(int hWnd, int Msg, int wParam, ref COPYDATASTRUCT lParam); public struct COPYDATASTRUCT { public IntPtr dwData; //可以是任意值 public int cbData; //指定lpData内存区域的字节数 [MarshalAs(UnmanagedType.LPStr)] public string lpData; //发送给目录窗口所在进程的数据 } public Form1() { InitializeComponent(); } private void btnEnum_Click(object sender, EventArgs e) { this.textBox1.Text = string.Empty; StringBuilder lpString = new StringBuilder(200); // 遍历所有windows EnumWindows(delegate (IntPtr hWnd, int lParam) { lpString = new StringBuilder(200); GetWindowTextW(hWnd, lpString, lpString.Capacity); if (!string.IsNullOrEmpty(lpString.ToString())) { this.textBox1.AppendText(lpString.ToString() "\r\n"); } return true; }, 0); } private void btnFimd_Click(object sender, EventArgs e) { hwnd = FindWindow(null, czct); //如果找到窗口句柄 if (hwnd != IntPtr.Zero) { this.textBox1.Clear(); this.textBox1.AppendText("找到:" czct); } else { this.textBox1.Clear(); this.textBox1.AppendText("未找到:" czct); } } private void button1_Click(object sender, EventArgs e) { this.textBox1.Clear(); } private void button2_Click(object sender, EventArgs e) { IntPtr hwnd = new IntPtr(0); IntPtr[] Ptr = new IntPtr[100]; hwnd = FindWindow(null, czct); if (hwnd != IntPtr.Zero) { int i = 0; int j = 0; EnumChildWindows(hwnd, delegate (IntPtr hWnd, int lParam) { StringBuilder lpString = new StringBuilder(200); GetClassName(hWnd, lpString, lpString.Capacity); String[] PtrClassName = new String[100]; if (!string.IsNullOrEmpty(lpString.ToString())) { this.textBox1.AppendText(lpString.ToString() "\r\n" hWnd.ToString() "\r\n" j.ToString() "\r\n"); j ; Ptr[i] = hWnd; i ; } return true; }, 0); //SendMessage(Ptr[33], WM_SETTEXT, 0, "测试发送数据1"); //SendMessage(Ptr[31], WM_SETTEXT, 0, "测试发送数据2"); //SendMessage(Ptr[36], WM_SETTEXT, 0, "测试发送数据3"); //SendMessage(Ptr[38], WM_SETTEXT, 0, "测试发送数据4"); //MessageBox.Show(Ptr[44] "已找到句柄1" Ptr[38] "已找到句柄2" Ptr[39] "已找到句柄3" Ptr[40] "已找到句柄4" Ptr[41] "已找到句柄1"); } else { MessageBox.Show("请打开开票软件界面"); } } }}
下载C# 遍历桌面窗口实例源码(windows API)用户还喜欢
- 18480 文章数
- 500万+ 热度
作者专栏
编辑推荐
- 淡抹u2引擎,修复内容较多,物有所值
- 界域传说·经典巨作=传世单机(一键安装)
- 丸子版本(175个传世版本大集合)
- GS版本:神话公益服务端+客户端
- 图片放大工具(放大图片不模糊)
- 剪映无限制VIP版
- 传奇世界客户端下载器,史上最全传世客户端
- 传世GS20220920商业引擎注册+登录配置器 解压密码是1
- U2官方排行榜游戏网关 支持元神,支持传家宝
- GS开战传世客户端+服务端
- (淡漠夕阳)u2引擎合区工具
- 传世GS引擎消除“你的游戏客户端版本号过旧,请及时更新”提示
- 传世一机多区双线路配置器--免密码版本
- 传世凤凰登陆器劫持修复软件
- SQLite3 for Navicat
- 传奇世界npc对话框编辑工具
- 传世GS落霞铭文服务器端
- gs_20210409引擎包+注册机(无限制)
- 传奇世界NPC对话封包查看器[支持时长版和极速版]
- 彩虹引擎传世脚本编辑工具1.7版来了,支持函数脚本翻译
评论